package com.zeyi.chat.handler;

import com.alibaba.fastjson.JSON;
import com.zeyi.chat.annotation.Mapping;
import com.zeyi.chat.dto.Chat;
import com.zeyi.chat.dto.Message;
import com.zeyi.chat.manager.SocketManager;
import com.zeyi.chat.po.ChatGroup;
import com.zeyi.chat.po.User;
import com.zeyi.chat.service.GroupService;
import com.zeyi.chat.service.UserService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zeyi
 */
public class ChatHandler {

    private ChatHandler() {
    }

    private static class Singleton {
        public static final ChatHandler INSTANCE = new ChatHandler();
    }

    public static ChatHandler getInstance() {
        return ChatHandler.Singleton.INSTANCE;
    }

    /**
     * 获取组息
     */
    @Mapping("groupInfo")
    public Message handleGroupInfo(Message message) {
        Integer groupId = Integer.valueOf(message.getData());
        ChatGroup group = GroupService.getInstance().getGroupById(groupId);
        return new Message(true, message.getHandlePath(), JSON.toJSONString(group));
    }

    /**
     * 获取用户信息
     */
    @Mapping("userInfo")
    public Message handleUserInfo(Message message) {
        Integer userId = Integer.valueOf(message.getData());
        User user = UserService.getInstance().getUserById(userId);
        return new Message(true, message.getHandlePath(), JSON.toJSONString(user));
    }

    /**
     * 删除聊天
     */
    @Mapping("deleteChat")
    public Message handleDelete(Message message) {
        Map<String, Integer> map = JSON.parseObject(message.getData(), Map.class);
        Integer type = map.get("type");
        Integer userId = map.get("userId");
        Integer deleteId = map.get("deleteId");
        Map<String, Integer> returnMap = new HashMap<>();
        returnMap.put("type", type);
        returnMap.put("deleteId", deleteId);
        Message newMessage = null;
        if (type == 0) {
            UserService.getInstance().deleteFriend(userId, deleteId);
            TaskHandler.submit(() -> {
                Map<String, Integer> map2 = new HashMap<>();
                map2.put("type", 0);
                map2.put("deleteId", userId);
                SocketManager.send(deleteId, new Message(true, message.getHandlePath(), JSON.toJSONString(map2)));
            });
            newMessage = new Message(true, message.getHandlePath(), JSON.toJSONString(returnMap));
        } else if (type == 1) {
            TaskHandler.submit(() -> {
                List<User> groupMember = GroupService.getInstance().getGroupMember(deleteId);
                GroupService.getInstance().deleteById(deleteId);
                groupMember.forEach(member -> {
                    Map<String, Integer> map2 = new HashMap<>();
                    map2.put("type", 1);
                    map2.put("deleteId", deleteId);
                    SocketManager.send(member.getId(), new Message(true, message.getHandlePath(), JSON.toJSONString(map2)));
                });
            });
        } else {
            TaskHandler.submit(() -> {
                GroupService.getInstance().deleteMember(deleteId, userId);
                Map<String, Integer> map2 = new HashMap<>();
                map2.put("type", 2);
                map2.put("deleteId", deleteId);
                SocketManager.send(userId, new Message(true, message.getHandlePath(), JSON.toJSONString(map2)));
            });
        }
        return newMessage;
    }

    /**
     * 修改用户信息
     */
    @Mapping("updateUserInfo")
    public Message handleUserUpdate(Message message) {
        User user = JSON.parseObject(message.getData(), User.class);
        UserService.getInstance().updateInfo(user);
        return null;
    }

    /**
     * 修改群信息
     */
    @Mapping("updateGroupInfo")
    public Message handleGroupUpdate(Message message) {
        ChatGroup group = JSON.parseObject(message.getData(), ChatGroup.class);
        GroupService.getInstance().updateInfo(group);
        return null;
    }

    /**
     * 处理私聊
     */
    @Mapping("chatPrivate")
    public Message handlePrivate(Message message) {
        TaskHandler.submit(() -> {
            User sender = message.getSender();
            Chat receiver = message.getReceiver();
            Message msg = new Message(message.getHandlePath(), sender, message.getData());
            SocketManager.send(receiver.getId(), msg);
        });
        return null;
    }

    /**
     * 处理群聊
     */
    @Mapping("chatGroup")
    public Message handleGroup(Message message) {
        User sender = message.getSender();
        Chat receiver = message.getReceiver();
        TaskHandler.submit(() -> {
            Message msg = new Message(message.getHandlePath(), sender, receiver, message.getData());
            //查找改组所有用户
            List<User> groupMember = GroupService.getInstance().getGroupMember(receiver.getId());
            groupMember.forEach(member -> {
                if (!member.getId().equals(sender.getId())) {
                    SocketManager.send(member.getId(), msg);
                }
            });
        });
        return null;
    }

    /**
     * 处理获取群成员
     */
    @Mapping("getGroupMember")
    public Message handleGroupMember(Message message) {
        Integer groupId = Integer.valueOf(message.getData());
        List<User> groupMembers = GroupService.getInstance().getGroupMember(groupId);
        ChatGroup group = new ChatGroup();
        group.setId(groupId);
        group.setMembers(groupMembers);
        return new Message(true, message.getHandlePath(), JSON.toJSONString(group));
    }
}
